package AccountWindow.SystemAdmin.RoomDetailWindow;

import AccountWindow.SystemAdmin.Account;
import AccountWindow.SystemAdmin.AccountDetailWindow.AccountDetailWindow;
import AccountWindow.SystemAdmin.AccountDetailWindow.Dao.AccountDetailDao;
import AccountWindow.SystemAdmin.AccountDetailWindow.TableCellRendererButtonOfDelete;
import AccountWindow.SystemAdmin.RoomDetailWindow.Dao.RoomDetailDao;
import SignAndRegister.RemindWindow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

public class RoomDetailWindow extends JPanel {

    private JPanel roomDetailPanel;
    private JPanel roomInformationPanel;
    private JTextField nowRoomIdField;
    private JComboBox nowRoomTypeBox;
    private JComboBox nowRoomStatusBox;
    private JTextField nowRoomCodeField;
    private JComboBox<String> typeBox;
    private JComboBox<String> statusBox;
    private JPanel roomTablePanel;
    private JScrollPane roomScrollPane;
    private JTable roomTable;
    private Vector<Vector> roomData;
    private JPanel roomButtonPanel;

    public RoomDetailWindow(String nowAccountID){
        roomDetailPanel = new JPanel(new BorderLayout());
        this.add(roomDetailPanel);

        roomInformationPanel = new JPanel(null);
        createRoomInformationPanel();

        roomTablePanel = new JPanel(new GridLayout(0,1));
        createRoomTablePanel();

        roomButtonPanel = new JPanel(new FlowLayout());
        creteRoomButtonPanel();

        roomInformationPanel.setPreferredSize(new Dimension(600,100));
        roomTablePanel.setPreferredSize(new Dimension(600,320));
        roomButtonPanel.setPreferredSize(new Dimension(600,100));

        roomDetailPanel.add(roomInformationPanel,BorderLayout.NORTH);
        roomDetailPanel.add(roomTablePanel,BorderLayout.CENTER);
        roomDetailPanel.add(roomButtonPanel,BorderLayout.SOUTH);


    }
    private boolean noFirstFlag = false;
    private JTable getRoomTable() throws Exception {
        Vector roomTableheaders = new Vector<>();
        roomTableheaders.add("客房编号");
        roomTableheaders.add("客房密码");
        roomTableheaders.add("客房类型");
        roomTableheaders.add("客房状态");

        if (noFirstFlag) {
            roomData.clear();   //如果不是第一次进入，则将data内所有数据清空
        }
        noFirstFlag = true;
        roomData = new RoomDetailDao().query();
        JTable jTable = new JTable(roomData, roomTableheaders);
        jTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int beSelectedRow = jTable.getSelectedRow();
                nowRoomIdField.setText(jTable.getValueAt(beSelectedRow,0).toString());
                nowRoomCodeField.setText(jTable.getValueAt(beSelectedRow,1).toString());
                nowRoomTypeBox.setSelectedItem(jTable.getValueAt(beSelectedRow,2).toString());
                nowRoomStatusBox.setSelectedItem(jTable.getValueAt(beSelectedRow,3).toString());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        return jTable;
    }

    private void refreshRoomTable(){
        if(noFirstFlag){
            roomTablePanel.remove(roomScrollPane);
        }
        noFirstFlag = true;
        roomScrollPane = new JScrollPane(roomTable);
        roomTablePanel.add(roomScrollPane);
        roomTablePanel.updateUI();
        roomTablePanel.invalidate();
        roomTablePanel.validate();
        roomTablePanel.repaint();
    }

    private void createRoomTablePanel(){
        try {
            roomTable = getRoomTable();
        } catch (Exception e) {
            e.printStackTrace();
        }
        roomScrollPane = new JScrollPane(roomTable);
        roomTablePanel.add(roomScrollPane);
    }

    private void creteRoomButtonPanel(){
        JButton searchRoomButton = new JButton("查询");
        JButton addRoomButton = new JButton("添加");
        JButton modifyRoomButton = new JButton("修改");
        JButton deleteRoomButton = new JButton("删除");

        searchRoomButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String roomID = nowRoomIdField.getText();
                try {
                    Room searchRoom = new RoomDetailDao().queryByID(roomID);
                    if(searchRoom!=null){
                        nowRoomCodeField.setText(searchRoom.roomCode);
                        nowRoomTypeBox.setSelectedItem(searchRoom.roomType);
                        nowRoomStatusBox.setSelectedItem(searchRoom.roomStatus);
                    }else {
                        new RemindWindow("NoThisRoom");
                    }
                }catch (Exception error){
                    error.printStackTrace();
                }
            }
        });

        addRoomButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String addRoomID = nowRoomIdField.getText();
                String addRoomCode = nowRoomCodeField.getText();
                String addRoomType = nowRoomTypeBox.getSelectedItem().toString();
                String addRoomStatus = "无人入住";
                Room addRoom = new Room(addRoomID,addRoomCode,addRoomType,addRoomStatus);

                if(addRoomID.length()==0){
                    new RemindWindow("PleaseFillRoomID");
                } else if (addRoomType.length()==0) {
                    new RemindWindow("PleaseFillRoomType");
                } else if (addRoomCode.length()>6) {
                    new RemindWindow("PleaseFillCodeCorrectly");
                } else {
                    try{
                        Room isExisitingRoom = new RoomDetailDao().queryByID(addRoomID);

                        if(isExisitingRoom != null){
                            new RemindWindow("AddingRoomFailure");
                        }else{
                            int isAddRoomSuccess = new RoomDetailDao().add(addRoom);
                            if(isAddRoomSuccess != 1){
                                new RemindWindow("AddingRoomFailure");
                            }
                        }
                        roomTable = getRoomTable();
                        refreshRoomTable();
                    }catch (Exception error){
                        error.printStackTrace();
                    }
                }
            }
        });

        modifyRoomButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String modifyRoomID = nowRoomIdField.getText();
                String modifyRoomCode = nowRoomCodeField.getText();
                String modifyRoomType = nowRoomTypeBox.getSelectedItem().toString();
                String modifyRoomStatus = nowRoomStatusBox.getSelectedItem().toString();
                Room modifyRoom = new Room(modifyRoomID,modifyRoomCode,modifyRoomType,modifyRoomStatus);
                if(modifyRoomID.length()==0){
                    new RemindWindow("PleaseFillRoomID");
                }else if(modifyRoomType.equals("")){
                    new RemindWindow("PleaseChooseRoomTypeCorrectly");
                }else if(modifyRoomStatus.equals("")){
                    new RemindWindow("PleaseChooseRoomStatusCorrectly");
                }else if(modifyRoomCode.length()!=6) {
                    new RemindWindow("PleaseFillCodeCorrectly");
                }else{
                    try{
                        int isModifySuccess = new RoomDetailDao().modify(modifyRoom);
                        if(isModifySuccess != 1){
                            new RemindWindow("ModifyingRoomFailure");
                        }
                        roomTable = getRoomTable();
                        refreshRoomTable();
                    }catch (Exception exception){
                        exception.printStackTrace();
                    }
                }
            }
        });

        deleteRoomButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String deleteRoomID = nowRoomIdField.getText();
                String deleteRoomCode = nowRoomCodeField.getText();
                String deleteRoomType = nowRoomTypeBox.getSelectedItem().toString();
                String deleteRoomStatus = nowRoomStatusBox.getSelectedItem().toString();
                Room deleteRoom = new Room(deleteRoomID,deleteRoomCode,deleteRoomType,deleteRoomStatus);
                try{
                    Room isExisitingRoom = new RoomDetailDao().queryByID(deleteRoomID);

                    if(isExisitingRoom != null){
                        int isDeleteSuccess = new RoomDetailDao().remove(deleteRoom);
                        roomTable = getRoomTable();
                        refreshRoomTable();
                    }else{
                        new RemindWindow("NoThisRoom");
                    }
                }catch (Exception exception){
                    exception.printStackTrace();
                }
            }
        });

        roomButtonPanel.add(searchRoomButton);
        roomButtonPanel.add(addRoomButton);
        roomButtonPanel.add(modifyRoomButton);
        roomButtonPanel.add(deleteRoomButton);
    }

    private void createRoomInformationPanel(){
        JLabel nowRoomIdLabel = new JLabel("客房编号:");
        nowRoomIdField = new JTextField(10);
        roomInformationPanel.add(nowRoomIdLabel);
        roomInformationPanel.add(nowRoomIdField);

        JLabel nowRoomTypeLabel = new JLabel("客房类型:");
        JPanel nowRoomTypePanel = new JPanel(null);
        nowRoomTypeBox = new JComboBox<String>();
        nowRoomTypeBox.addItem("");
        nowRoomTypeBox.addItem("单人房");
        nowRoomTypeBox.addItem("双人房");
        nowRoomTypeBox.addItem("大床房");
        nowRoomTypeBox.addItem("家庭套房");
        nowRoomTypeBox.addItem("豪华套房");
        nowRoomTypePanel.add(nowRoomTypeBox);
        nowRoomTypeBox.setBounds(0,0,200,20);
        roomInformationPanel.add(nowRoomTypeLabel);
        roomInformationPanel.add(nowRoomTypePanel);

        JLabel nowRoomStatusLabel = new JLabel("客房状态:");
        JPanel nowRoomStatusPanel = new JPanel(null);
        nowRoomStatusBox = new JComboBox<String>();
        nowRoomStatusBox.addItem("");
        nowRoomStatusBox.addItem("无人入住");
        nowRoomStatusBox.addItem("有人入住");
        nowRoomStatusBox.addItem("退房清洁");
        nowRoomStatusPanel.add(nowRoomStatusBox);
        nowRoomStatusBox.setBounds(0,0,200,20);
        roomInformationPanel.add(nowRoomStatusLabel);
        roomInformationPanel.add(nowRoomStatusPanel);

        JLabel nowRoomCodeLabel = new JLabel("客房密码:");
        nowRoomCodeField = new JTextField(10);
        roomInformationPanel.add(nowRoomCodeLabel);
        roomInformationPanel.add(nowRoomCodeField);

        JLabel onlyLookRoomLabel = new JLabel("仅查类型:");
        JPanel roomTypeChoicePanel = new JPanel(null);
        typeBox = new JComboBox<String>();
        typeBox.addItem("任何类型");
        typeBox.addItem("单人房");
        typeBox.addItem("双人房");
        typeBox.addItem("大床房");
        typeBox.addItem("家庭套房");
        typeBox.addItem("豪华套房");
        roomTypeChoicePanel.add(typeBox);
        typeBox.setBounds(0,0,200,20);
        typeBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedType = typeBox.getSelectedItem().toString();
                String selectedStatus = statusBox.getSelectedItem().toString();
                Room selectedRoom = new Room(selectedType,selectedStatus);
                try{
                    filterRoomTableByTypeAndStatus(selectedRoom);
                    refreshRoomTable();
                }catch (Exception error){
                    error.printStackTrace();
                }
            }
        });
        roomInformationPanel.add(onlyLookRoomLabel);
        roomInformationPanel.add(roomTypeChoicePanel);

        JLabel onlyLookStatusLabel = new JLabel("仅查状态:");
        JPanel roomStatusChoicePanel = new JPanel(null);
        statusBox = new JComboBox<String>();
        statusBox.addItem("任何状态");
        statusBox.addItem("无人入住");
        statusBox.addItem("有人入住");
        statusBox.addItem("退房清洁");
        roomStatusChoicePanel.add(statusBox);
        statusBox.setBounds(0,0,200,20);
        statusBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedType = typeBox.getSelectedItem().toString();
                String selectedStatus = statusBox.getSelectedItem().toString();
                Room selectedRoom = new Room(selectedType,selectedStatus);
                try{
                    filterRoomTableByTypeAndStatus(selectedRoom);
                    refreshRoomTable();
                }catch (Exception error){
                    error.printStackTrace();
                }
            }
        });
        roomInformationPanel.add(onlyLookStatusLabel);
        roomInformationPanel.add(roomStatusChoicePanel);

        nowRoomIdLabel.setBounds(0,5,100,20);
        nowRoomIdField.setBounds(60,5,200,20);
        nowRoomTypeLabel.setBounds(280,5,100,20);
        nowRoomTypePanel.setBounds(340,5,200,20);
        nowRoomStatusLabel.setBounds(0,35,100,20);
        nowRoomStatusPanel.setBounds(60,35,200,20);
        nowRoomCodeLabel.setBounds(280,35,100,20);
        nowRoomCodeField.setBounds(340,35,200,20);
        onlyLookRoomLabel.setBounds(0,65,100,20);
        roomTypeChoicePanel.setBounds(60,65,200,20);
        onlyLookStatusLabel.setBounds(280,65,100,20);
        roomStatusChoicePanel.setBounds(340,65,200,20);
    }

    private void filterRoomTableByTypeAndStatus(Room selectedRoom) throws Exception {
        roomData.clear();
        roomData = new RoomDetailDao().queryByTypeAndStatus(selectedRoom);
        Vector roomTableheaders = new Vector<>();
        roomTableheaders.add("客房编号");
        roomTableheaders.add("客房密码");
        roomTableheaders.add("客房类型");
        roomTableheaders.add("客房状态");
        roomTable = new JTable(roomData,roomTableheaders);
        roomTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int beSelectedRow = roomTable.getSelectedRow();
                nowRoomIdField.setText(roomTable.getValueAt(beSelectedRow,0).toString());
                nowRoomCodeField.setText(roomTable.getValueAt(beSelectedRow,1).toString());
                nowRoomTypeBox.setSelectedItem(roomTable.getValueAt(beSelectedRow,2).toString());
                nowRoomStatusBox.setSelectedItem(roomTable.getValueAt(beSelectedRow,3).toString());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

}
